﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace TinyCinema.Bussiness.Menu
{
    public class Order : INotifyPropertyChanged
    {
        public Data.Menu Menu { get; set; }
        public decimal Value { get; set; }
        int _Quantumn;
        public int Quantumn { 
            get { return _Quantumn; }
            set 
            {
                if (value <= 0) value=1;
                _Quantumn = value;
                Value = value * Menu.Price;                
                OnPropertyChanged("Value");
                OnPropertyChanged("Quantumn");
            }
        }        
        public Order(Data.Menu _Menu)
        {
            Menu = _Menu;
            Quantumn = 1;
            
        }
        public KeyValuePair<Data.Menu,int> ToKeyValuePair()
        {
            return new KeyValuePair<Data.Menu, int>(Menu, Quantumn);
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public class Deal : System.Windows.DependencyObject
    {
        public static DependencyProperty MenusProperty = DependencyProperty.Register("Menus", typeof(ObservableCollection<Data.Menu>), typeof(Deal));
        public ObservableCollection<Data.Menu> Menus
        {
            get { return (ObservableCollection<Data.Menu>)GetValue(MenusProperty); }
            set { SetValue(MenusProperty, value); }
        }
        public static DependencyProperty OrdersProperty = DependencyProperty.Register("Orders", typeof(ObservableCollection<Order>), typeof(Deal));
        public ObservableCollection<Order> Orders
        {
            get { return (ObservableCollection<Order>)GetValue(OrdersProperty); }
            set { SetValue(OrdersProperty, value); }
        }
        public static DependencyProperty TotalValueProperty = DependencyProperty.Register("TotalValue", typeof(decimal), typeof(Deal));
        public decimal TotalValue
        {
            get { return (decimal)GetValue(TotalValueProperty); }
            set { SetValue(TotalValueProperty, value); }
        }
        public static DependencyProperty CustomerIDOrderingProperty = DependencyProperty.Register("CustomerIDOrdering", typeof(string), typeof(Deal));
        public string CustomerIDOrdering
        {
            get { return (string)GetValue(CustomerIDOrderingProperty); }
            set
            {
                SetValue(CustomerIDOrderingProperty, value);
            }
        }
        public Messenger Messenger { get; set; }
        public ICommand AddMenuCommand { get; set; }
        public ICommand RemoveMenuCommand { get; set; }
        public ICommand CancelCommand { get; set; }
        public ICommand PrintCommand { get; set; }
        public ICommand PayCommand { get; set; }
        public ICommand CheckCustomerIDCommand { get; set; }

        public Deal()
        {
            Messenger = new Bussiness.Messenger();
            Menus = Data.Menu.GetAllMenu();
            Orders = new ObservableCollection<Order>();
            AddMenuCommand = new RelayCommand<Data.Menu>(
                (s) => true,
                (s) =>
                {
                    var check = false;
                    foreach (var Order in Orders)
                        if (s.ID == Order.Menu.ID)
                        {
                            ++Order.Quantumn;
                            check = true;
                            break;
                        }
                    if (!check) Orders.Add(new Order(s));

                    TotalValue = 0;
                    foreach (var Order in Orders)
                        TotalValue += Order.Value;
                }
            );
            RemoveMenuCommand = new RelayCommand<Bussiness.Menu.Order>(
                (s) => true,
                (s) =>
                {
                    Orders.Remove(s);
                    TotalValue = 0;
                    foreach (var Order in Orders)
                        TotalValue += Order.Value;                   
                }
            );

            CancelCommand = new RelayCommand<Bussiness.Menu.Order>(
                 (s) => Orders.Count > 0,
                 (s) =>
                 
                 {
                     var message1 = new Message.DeleteConfirmation();
                     Messenger.Send(message1);
                     if (message1.Result == MessageBoxResult.Yes)
                         try
                         {
                             Orders.Clear();
                             TotalValue = 0;
                         }
                         catch { }
                 }
             );
            PayCommand = new RelayCommand<Data.Employee>(
                 (s) => Orders.Count > 0,
                 (s) =>
                 {
                     var message = new Message.PayConfirmation();
                     Messenger.Send(message);
                     if(message.Result==MessageBoxResult.Yes)
                     {
                         Data.Customer customer = null;
                         try
                         {
                             customer = new Data.Customer(CustomerIDOrdering);
                         }
                         catch { }
                         var OrderDetail = new ObservableCollection<KeyValuePair<Data.Menu, int>>();
                         foreach (var Order in Orders)
                             OrderDetail.Add(Order.ToKeyValuePair());
                         var x = new Data.MenuOrder(customer, s, OrderDetail);
                         Messenger.Send(new Message.PayConfirmationOK());
                     }
                 }
             );
            PrintCommand = new RelayCommand<Bussiness.Menu.Order>(
                 (s) => true,
                 (s) =>
                {
                    try
                    {
                        Orders.Clear();
                        TotalValue = 0;
                    }
                    catch { }
                 }
             );
            CheckCustomerIDCommand = new RelayCommand<string>(
                      (s) => true,
                      (s) =>
                      {
                          CustomerIDOrdering = s;
                          try
                          {
                              var Customer = new Data.Customer(CustomerIDOrdering);
                              Messenger.Send(new Message.CustomerIDFound());
                          }
                          catch
                          {
                              Messenger.Send(new Message.CustomerIDNotFound());
                          }
                      });

        }
        public void ValueCount()
        {
            TotalValue = 0;
            foreach (var Order in Orders)
                TotalValue += Order.Value; 
        }
    }
}
